package 回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.cn/problems/letter-combinations-of-a-phone-number/?favorite=2cktkvj
 *
 * https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247493564&idx=1&sn=1e7614edc86b0c36747c5ac3c86f1610&chksm=9bd419b4aca390a2eaa42560caac36a6828726294c76dac610809137394c1855c46f6c4717ce&scene=178&cur_album_id=2165135739779448832#rd
 */
public class _17_电话号码的字母组合 {

    private List<String> nums = new ArrayList<>(Arrays.asList("abc", "def", "ghi", "jkl", "mno", "qprs", "tuv", "wxyz"));
    List<String> resultList;
    List<Character> characters;
    char[] digits;

    public List<String> letterCombinations(String digits) {
        resultList = new ArrayList<>();
        characters = new ArrayList<>();
        this.digits = digits.toCharArray();
        if(digits == null || digits.length() ==0) return resultList;
        dfs(0);
        return resultList;
    }


    private void dfs(int position) {
        // base case
        if (characters.size() == digits.length) {
            char[] result = new char[digits.length];
            for (int i = 0; i < characters.size(); i++) {
                result[i] = characters.get(i);
            }
            resultList.add(new String(result));
            return;
        }

        // 这边是重点，怎么根据position取到相关的char[]
        char[] chars = nums.get(digits[position] - '2').toCharArray();
        for (int i = 0; i < chars.length; i++) {
            characters.add(chars[i]);
            dfs(position + 1);
            characters.remove(characters.size() - 1);
        }
    }
}
